package com.ngm.tea.t.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ngm.tea.common.PageInfo;
import com.ngm.tea.t.dao.AccountDao;
import com.ngm.tea.t.entity.Account;
import com.ngm.tea.t.entity.AccountRole;
import com.ngm.tea.t.service.AccountRoleService;
import com.ngm.tea.t.service.AccountService;

/**
 * 服务层
 * @author zjw
 * @version 1.0
 * @date 2016-09-01 21:07:07
 *
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService {
	
	@Resource
	private AccountDao accountDao;
	@Resource
	private AccountRoleService accountRoleService;

	public AccountDao getAccountDao() {
		return accountDao;
	}

	@Override
	public int insert(Account account) {
		return accountDao.insert(account);
	}

	@Override
	public void deleteById(Long id) {
		Account account = this.selectOne(id);
		String accCode = account.getAccCode();
		accountRoleService.deleteByAccCode(accCode);
		accountDao.deleteById(id);
	}

	@Override
	public void deleteByIds(String ids) {
		String[] _ids = ids.split(",");
		if (_ids.length > 0) {
			List<String> idlist = Arrays.asList(_ids);
			accountDao.deleteByIds(idlist);
		}
	}

	@Override
	public void delete(Account account) {
		accountDao.delete(account);
	}

	@Override
	public void update(Account account,String roleCode) {
		AccountRole accountRole = accountRoleService.selectOneByAccCode(account.getAccCode());
		accountRole.setRoleCode(roleCode);
		accountRoleService.update(accountRole);
		accountDao.update(account);
	}

	@Override
	public Account selectOne(Long id) {
		return accountDao.selectOne(id);
	}
	
	@Override
	public Account selectByNamePassword(Account account) {
		return accountDao.selectByNamePassword(account);
	}

	@Override
	public List<Account> selectAll() {
		return accountDao.selectAll();
	}

	@Override
	public List<Account> selectList(Account account, PageInfo pageInfo) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(account));
		map.put("pageInfo", pageInfo);
		map.put("order", pageInfo.getOrderField()+" "+pageInfo.getOrderDirection());
		return accountDao.selectList(map);
	}

	@Override
	public Long count(Account account) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(account));
		return accountDao.count(map);
	}
	
	public String makeStr(Account account) {
		StringBuffer sb = new StringBuffer();
		if(!StringUtils.isEmpty(account.getId())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("t_account.id LIKE '%" + account.getId()+"%'");
			} else {
				sb.append(" AND t_account.id LIKE '%" + account.getId()+"%'");
			}
		}
		if(!StringUtils.isEmpty(account.getAccName())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("t_account.acc_name LIKE '%" + account.getAccName()+"%'");
			} else {
				sb.append(" AND t_account.acc_name LIKE '%" + account.getAccName()+"%'");
			}
		}
		if (StringUtils.isEmpty(sb.toString())) {
			return null;
		} else {
			return sb.toString();
		}
	}

	@Override
	public List<Map<String, Object>> selectRll(Account account, PageInfo pageInfo) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(account));
		map.put("pageInfo", pageInfo);
		map.put("order", pageInfo.getOrderField()+" "+pageInfo.getOrderDirection());
		return accountDao.selectRll(map);
	}

	@Override
	public List<Map<String, String>> roleList(Account account) {
		HashMap<String, String> map = new HashMap<>();
		map.put("acc_code", account.getAccCode());
		return accountDao.roleList(map);
	}
	
}